<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- <link rel="stylesheet" href="../css/bootstrap.min.css"> -->
		<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
		<style>
			.img-responsive {
			  display: inline-block;
			  height: auto;
			  max-width: 100%;
			}
		</style>
		<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
		<script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
	</head>
	<body>
		<nav class="navbar navbar-inverse" role="navigation">
			<div class="navbar-header">
				<a class="navbar-brand" href="#" style="color: white;">iPhone串号截图生成器</a>
			</div>
		</nav>
		<div class="container">
			<textarea id="imeitxt" style="width: 100%;height: 15rem;" placeholder="在此输入串号，每行一个"></textarea>
			<p>
				<button type="button" class="btn btn-success btn-lg btn-block" onclick="getImgs()">生成图片</button>
			</p>
			<div style="margin-top:1rem;">
				<div class="panel-group" id="accordion">
					<div id="toGen">
						<h2 id="hints">结果区</h2>
					</div>
				</div>
			</div>
		</div>

		<script>
			let purl = "http://" + window.location.host + "/api/getImg"
			// let purl = "http://localhost:22222/api/getImg";

			function getImgs() {
				let imeis = $("#imeitxt").val().split("\n");
				$("#hints").html("正在生成，请等待");
				$.ajax({
					url: purl,
					type: 'post',
					dataType: 'json',
					data: JSON.stringify({
						"IMEIs": imeis,
						"isRandom": 0
					}),
					headers: {
						"Content-Type": "application/json"
					},
					success: (data) => {
						if (data.code === 0) {
							$("#imeitxt").val("");
							$("#toGen").html("");
							let divs = "";
							for (let i = 0; i < data.data.length; i++) {
								let dt = data.data[i];
								let imei = dt.imei;
								let picBase64 = dt.picBase64;
								divs = divs + "<div class='panel panel-default'><div class='panel-heading'><h4 class='panel-title'>";
								divs = divs + "<a data-toggle='collapse' data-parent='#accordion' href='#collapse" + i + "'>";
								divs = divs + imei;
								divs = divs + "</a></h4></div>";
								if (i == 0) {
									// divs = divs + "<div id='collapse" + i + "' class='panel-collapse collapse in'>";
									divs = divs + "<div id='collapse" + i + "' class='panel-collapse collapse'>";
								} else {
									divs = divs + "<div id='collapse" + i + "' class='panel-collapse collapse'>";
								}
								divs = divs + "<div class='panel-body'>";
								if (picBase64 === "-1") {
									divs = divs + "<p>无效的iPhone IMEI</p>";
								} else if (picBase64 === "-2") {
									divs = divs + "<p>无效的IMEI</p>";
								} else {
									divs = divs + "<img class='img-responsive' src='" + picBase64 + "' />"
								}
								divs = divs + "</div></div></div>";
							}
							$("#toGen").html(divs);
						}
					},
					error: (data) => {
						console.log("error");
					}
				});

			}
		</script>

		<!-- <script src="../js/jquery.min.js"></script>
		<script src="../js/bootstrap.min.js"></script> -->
	</body>
</html>
